namespace cnVery.Cms
{
    using cnVery.Data;
    using cnVery.Utils;
    using System;
    using System.Data;
    using System.Text;

    public class websites
    {
        public static void Add(website model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into websites(");
            builder.Append("categoryid,username,[name],url,logo,[country],[language],[description],[time],clickcount,vouch,[approved],[deleted]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.categoryid + ",");
            builder.Append("'" + model.username + "',");
            builder.Append("'" + model.name + "',");
            builder.Append("'" + model.url + "',");
            builder.Append("'" + model.logo + "',");
            builder.Append("'" + model.country + "',");
            builder.Append("'" + model.language + "',");
            builder.Append("'" + model.description + "',");
            builder.Append("'" + model.time + "',");
            builder.Append("" + model.clickcount + ",");
            builder.Append("" + (model.vouch ? 1 : 0) + ",");
            builder.Append("" + (model.approved ? 1 : 0) + ",");
            builder.Append("" + (model.deleted ? 1 : 0) + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(website model)
        {
            Tools.deletefile(model.logo);
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete websites ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from websites where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int) scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from websites where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int) scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from websites ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from websites where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from websites where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static website GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from websites ");
            builder.Append(" where id=" + id);
            website website = new website();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            website.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            if (dataTable.Rows[0]["categoryid"].ToString() != "")
            {
                website.categoryid = int.Parse(dataTable.Rows[0]["categoryid"].ToString());
            }
            website.username = dataTable.Rows[0]["username"].ToString();
            website.name = dataTable.Rows[0]["name"].ToString();
            website.url = dataTable.Rows[0]["url"].ToString();
            website.logo = dataTable.Rows[0]["logo"].ToString();
            website.country = dataTable.Rows[0]["country"].ToString();
            website.language = dataTable.Rows[0]["language"].ToString();
            website.description = dataTable.Rows[0]["description"].ToString();
            if (dataTable.Rows[0]["time"].ToString() != "")
            {
                website.time = DateTime.Parse(dataTable.Rows[0]["time"].ToString());
            }
            if (dataTable.Rows[0]["clickcount"].ToString() != "")
            {
                website.clickcount = int.Parse(dataTable.Rows[0]["clickcount"].ToString());
            }
            if (dataTable.Rows[0]["vouch"].ToString() != "")
            {
                if ((dataTable.Rows[0]["vouch"].ToString() == "1") || (dataTable.Rows[0]["vouch"].ToString().ToLower() == "true"))
                {
                    website.vouch = true;
                }
                else
                {
                    website.vouch = false;
                }
            }
            if (dataTable.Rows[0]["approved"].ToString() != "")
            {
                if ((dataTable.Rows[0]["approved"].ToString() == "1") || (dataTable.Rows[0]["approved"].ToString().ToLower() == "true"))
                {
                    website.approved = true;
                }
                else
                {
                    website.approved = false;
                }
            }
            if (dataTable.Rows[0]["deleted"].ToString() != "")
            {
                if ((dataTable.Rows[0]["deleted"].ToString() == "1") || (dataTable.Rows[0]["deleted"].ToString().ToLower() == "true"))
                {
                    website.approved = true;
                    return website;
                }
                website.approved = false;
            }
            return website;
        }

        public static void Update(website model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update websites set ");
            builder.Append("categoryid=" + model.categoryid + ",");
            builder.Append("username='" + model.username + "',");
            builder.Append("[name]='" + model.name + "',");
            builder.Append("url='" + model.url + "',");
            builder.Append("logo='" + model.logo + "',");
            builder.Append("[country]='" + model.country + "',");
            builder.Append("[language]='" + model.language + "',");
            builder.Append("[description]='" + model.description + "',");
            builder.Append("[time]='" + model.time + "',");
            builder.Append("clickcount=" + model.clickcount + ",");
            builder.Append("vouch=" + (model.vouch ? 1 : 0) + ",");
            builder.Append("[approved]=" + (model.approved ? 1 : 0) + ",");
            builder.Append("[deleted]=" + (model.deleted ? 1 : 0) + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Update(string sql, string ids)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update [websites] set " + sql + " where id in (" + ids + ")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }
}

